import requests
import json
# url = "https://www.baidu.com"
# response = requests.get(url)
# print(response.text)



#不带参数
# url = "http://www.baidu.com"
# response = requests.get(url)
# # print(response.text)
# # html = response.content.decode('utf-8')
# fail_path = "2.html"
# with open(fail_path, 'w', encoding='utf-8') as f:
#     f.write(response.t)


# https://www.sogou.com/web?query=java
# url = "https://www.sogou.com/web?query=java"
# response = requests.get(url=url)
# # print(response.text)
# html = response.content.decode('utf-8')
# fail_path = "1.html"
# with open(fail_path, 'w', encoding='utf-8') as f:
#     f.write(html)

# #带参数
# url = "https://www.sogou.com/web?query=java"
# response = requests.get(url=url)
# # print(response.text)
# html = response.content.decode('utf-8')
# fail_path = "1.html"
# with open(fail_path, 'w', encoding='utf-8') as f:
#     f.write(html)

# Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0

# url = "https://www.sogou.com/web?query=幸豪"
# #设置请求头
# headers = {
#     "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0",
#     "Referer": "https://www.sogou.com/",
# }
# response = requests.get(url=url,headers=headers)
# # print(response.text)
# html = response.content.decode('utf-8')
# fail_path = "1.html"
# with open(fail_path, 'w', encoding='utf-8') as f:
#     f.write(html)


# url = "https://www.sogou.com/web?"
# input_str = input("请输入搜索内容：")
# data = {"query": input_str}
# #设置请求头
# headers = {
#     "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0",
#     # "Referer": "https://www.sogou.com/",
# }
# response = requests.get(url=url,headers=headers,params=data)
# print(f'{response.url}')
# html = response.content.decode('utf-8')
# fail_path = "1.html"
# with open(fail_path, 'w', encoding='utf-8') as f:
#     f.write(html)


# # input_str = input("请输入搜索内容：")
# # data = {"query": input_str}
# #设置请求头
# headers = {
#     "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0",
#     # "Referer": "https://www.sogou.com/",
# }

# data = {
#     type:22,
#     limit:20,
#     start:0,
#     interval_id:"100:90"
# }
# response = requests.post(url=url,headers=headers,params=data)
# with open("result.json", 'w', encoding='utf-8') as f:
#     json.dump(response.json(),f)



# 请求参数（根据API文档调整）
# data = {
#     "type":17,
#     "interval_id":"100:90",
#     "action":"",
#     "start":0,
#     "limit":20
# }

# 发送POST请求（修正参数）
# 保存响应结果为JSON文件（确保响应内容为JSON格式）
# with open("result.json", "w", encoding="utf-8") as f:
#     json.dump(response.json(), f)  # ensure_ascii=False 避免中文乱码
# import requests
# import json
# import csv
# url = "https://movie.douban.com/j/chart/top_list"
# header = {
#     "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0"
# }
# start = 0
# movies = []
# for i in range(5):
#     data = {
#         "type":15,
#         "interval_id":"100:90",
#         "action":"",
#         "start":start,
#         "limit":20
#     }
#     response = requests.get(url=url, headers=header, params=data)  # 若服务器接受JSON格式，推荐用json参数
#     for item in response.json():
#         title = item["title"]
#         regions = item["regions"][0]
#         release_date = item["release_date"]
#         vote_count = item["vote_count"]
#         score = item["score"]
#         movie = [title, regions, release_date, vote_count, score]
#         movies.append(movie)
#     start += 20
# with open("movie2.csv", "w", encoding="utf-8") as f:
#     csv_writer = csv.writer(f)
#     csv_writer.writerow(["title","regions","release_date","vote_count","score"])
#     csv_writer.writerows(movies)



url = "https://www.sogou.com/web?"
input_str = input("请输入搜索内容：")
data = {"query": input_str}
#设置请求头
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0",
    # "Referer": "https://www.sogou.com/",
}
response = requests.get(url=url,headers=headers,params=data)
print(f'{response.url}')
html = response.content.decode('utf-8')
fail_path = "1111.html"
with open(fail_path, 'w', encoding='utf-8') as f:
    f.write(html)